import {getOrderInfoAPI, getUserAddressAPI, submitOrderAPI} from "@/api/tradeApi";


const actions = {
    /* 获取用户地址信息的actions */
    async fetchUserAddress(context) {
        const {data} = await getUserAddressAPI()
        context.commit("updateUserAddress", data)
    },
    /* 获取商品清单的actions */
    async fetchOrderInfo(context) {
        const {data} = await getOrderInfoAPI()
        context.commit("updateOrderInfo", data)

    },
    /* 提交订单的actions */
    async submitOrder(context, {tradeNo, data}) {
        const result = await submitOrderAPI(tradeNo, data)
        if (result.code !== 200) return Promise.reject("提交订单失败")
        return Promise.resolve(result.data)
    }
}
const mutations = {
    /* 更新用户地址信息的actions */
    updateUserAddress(state, address) {
        state.address = address
    },
    /* 更新商品清单的的mutations */
    updateOrderInfo(state, orderInfo) {
        state.orderInfo = orderInfo
    }
}
const state = () => {
    return {
        address: [],
        orderInfo: {}
    }
}
const getters = {}
export default {
    actions,
    mutations,
    state,
    getters,
    namespaced: true
}
